import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        Chain MyBlockChain = new Chain();
        System.out.println(MyBlockChain.toString());

        // 生成两对公私钥对
        KeyPair keyPair1 = ECCKeyGen.generateKeyPair();
        PrivateKey sigKey1 = keyPair1.getPrivate();
        PublicKey pubKey1 = keyPair1.getPublic();
        KeyPair keyPair2 = ECCKeyGen.generateKeyPair();
        PrivateKey sigKey2 = keyPair2.getPrivate();
        PublicKey pubKey2 = keyPair2.getPublic();
        // 将格式转为base64的字符串
        String sigKeyStr1 = ECCKeyGen.privateKeyToBase64((ECPrivateKey) sigKey1);
        String pubKeyStr1 = ECCKeyGen.publicKeyToBase64((ECPublicKey) pubKey1);
        String sigKeyStr2 = ECCKeyGen.privateKeyToBase64((ECPrivateKey) sigKey2);
        String pubKeyStr2 = ECCKeyGen.publicKeyToBase64((ECPublicKey) pubKey2);
        // System.out.println(sigKeyStr1);
        // System.out.println(pubKeyStr1);

        /**
         * MyBlockChain：在哪条链上挖矿
         * mineBlock：挖矿（挖区块）
         * minerAddress：谁在挖矿
         */
        MyBlockChain.mineBlock(pubKeyStr1);
        System.out.println(MyBlockChain.toString());
        System.out.println("================");

        // 手动创造交易，并将交易加入到交易池中。交易被创造时就应该被签名
        Transaction tx1 = new Transaction(pubKeyStr1,pubKeyStr2,22); // 公钥代替人
        Transaction tx2 = new Transaction(pubKeyStr2,pubKeyStr1,30);
        tx1.sign(sigKeyStr1);
        tx2.sign(sigKeyStr2);
        // 将交易接受到交易池中，等待被区块收录
        MyBlockChain.addTxToPool(tx1);
        MyBlockChain.addTxToPool(tx2);
        System.out.println(MyBlockChain.toString()); // tx1和tx2未被使用到， 因为没有一个区块直接通过传入transactionsPool而实例化

        MyBlockChain.createBlock(); // 这里的新区快看到了tx1和tx2
        System.out.println(MyBlockChain.toString());
//        MyBlockChain.createBlock(); // 空区块
//        System.out.println(MyBlockChain.toString());

        System.out.println(MyBlockChain.validateChain());
    }
}